/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zproekt.servis.OrdinacijaCRUD;
import com.zproekt.entity.Pacient;
import com.zproekt.entity.Termin;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author Ruzin
 */
@WebServlet(name = "dodadiTermin", urlPatterns = {"/dodadiTermin.do"})
public class dodadiTermin extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Pacient pacient = new Pacient();
        java.util.List<Termin> lista = OrdinacijaCRUD.getAllTermini();
        Calendar denes = Calendar.getInstance();
        denes.add(Calendar.DATE, -1);
        java.util.Date yesterday = new java.util.Date(denes.getTimeInMillis());
        pacient = OrdinacijaCRUD.getPacient(Long.valueOf(request.getParameter("idPacient")));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String myDate = request.getParameter("datum");
        Date datavn = null;
        try {
            datavn = formatter.parse(myDate);
        } catch (ParseException ex) {
            Logger.getLogger(dodadiTermin.class.getName()).log(Level.SEVERE, null, ex);
        }
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm");
        String myTime = request.getParameter("vreme");
        Date vremevn = null;
        try {
            vremevn = formatter2.parse(myTime);
        } catch (ParseException ex) {
            Logger.getLogger(dodadiTermin.class.getName()).log(Level.SEVERE, null, ex);
        }
        if ((datavn).before(yesterday)) {
            response.sendRedirect("dodadiTermin.jsp?error=pominato");
        } else {
            String novoVreme = myTime+":00";
            for (int i = 0; i < lista.size(); i++) {
            if ((myDate.trim().equals(lista.get(i).getDatum().toString()))&&(novoVreme.trim().equals(lista.get(i).getVreme().toString()))){
            response.sendRedirect("dodadiTermin.jsp?error=postoi");
            }
            }
            OrdinacijaCRUD.createTermin(datavn, vremevn, pacient, null);
            response.sendRedirect("admin.jsp?success=vnesenTermin");
            }

        try {
            /**
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet dodadiTermin</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet dodadiTermin at " + pacient.getIme() + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
